package seotools.positions;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: vladimir
 * Date: 17.05.11
 * Time: 22:58
 * To change this template use File | Settings | File Templates.
 */
public class MainGoogle {
    private static final long DELAY = 300;

    public static void main(String[] args) throws IOException, InterruptedException {
//        Document doc = Jsoup.connect("http://yandex.ru/yandsearch?p=" + page + "&text=" + phrase + "&lr=2").get();
//        Document doc = Jsoup.connect("http://www.google.ru/#q=постановка+свадебного+танца&hl=ru&start=0&fp=blabla").get();
//        String phrase = "постановка+свадебного+танца";
//        String url = "vitmar-dance.ru";
        String url = "1tanec.ru";
        String phrase = "свадебный+танец";
        for (int i = 0; i < 100; i += 100) {
            if (i != 0) {
                Thread.sleep(DELAY);
            }
//            Document doc = Jsoup.connect("http://www.google.com/search?sclient=psy&hl=ru&source=hp&q=" + phrase + "&btnG=search&start=" + i).
//                    header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").get();
            //http://www.google.ru/search?adCount=100&hl=ru&newwindow=1&biw=1280&bih=593&q=&aq=f&aqi=&aql=&oq=
//            String googleURL = "http://www.google.com/search?adCount=100&sclient=psy&hl=ru&source=hp&q=" + phrase + "&btnG=search&start=" + i;
            String googleURL = "http://www.google.ru/search?hl=ru&source=hp&biw=1280&bih=566&q="+phrase+"&aq=1&aqi=g10&aql=&oq=&start=" + i;
            Document doc = Jsoup.connect(googleURL).
                    header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").
                    header("Cookie","PREF=ID=ba43d045ae8fb44a:U=fdcd58c3e7ae19f9:FF=0:LD=ru:NR=100:NW=1:TM=1299345317:LM=1307794433:SG=2:S=yc6UGpylNNGBMS8u; NID=47=NXohthQxpzoceXA-JXJRptGBQc_cnRJzLeauru_HvnBJsAXNXmhR8wNyQQEah_4sNGKasyxbwo5XPUN2Av-dIef7W_M7x9wC9ECZqlxHqa6VdE2YqCqyXoZ3m8F1_sb4; HSID=ABJdKxI-fIR4G3ewf; SID=DQAAALgAAACmHoYtrB5YP9X1PkxWaYGz3b9U0x_16eTbhiw7mtpBWr061NkKWRggBgAQdX0Zr6oJMwu-UuEyMcS7F2D0ljLcsCr5hVrVFy-51EZ9E-OjcetgxB-DvR1BsLqMH_sNxchsf9mP93G1Tqkp3wBd8uTPdxX7Gc3HNdCkZMLR-f6OV4bSMhwofIcdMfBFZDBBjdByKwHwxfC9vABPAtj_6sF9muN7DHfHmr3VIOkOz19v0g2vMZ6TvHY4hpTTZXRye7E").get();
            Element res = doc.getElementById("rso");
            Elements cites = res.select("cite");
            System.out.println(cites.size());
            int exceptions = 0;
            int position = 0;
            for (Element cite : cites) {
                position++;
//                System.out.println(cite.text());
                if (isException(cite.text())) {
                    exceptions++;
                }
                System.out.println((position - exceptions + i) + "-" + cite.text());
//                if (cite.text().contains(url)) {
//                }
            }
        }
//        System.out.println(cites);

//        Map<Integer, String> positions = new HashMap<Integer, String>();
//        for (int page = 0; page < 10; page++) {
//            Document doc = null;
//            try {
//            } catch (SocketTimeoutException e) {
//                System.out.println(e);
//                Thread.sleep(500);
//                page--;
//                continue;
//            }
//            Element itemList = doc.select("ol.b-serp-list").get(0);
//            Elements items = itemList.select(".b-serp-item__title");
//            for (Element item : items) {
//                Integer position = Integer.parseInt(item.child(0).text());
//                String url = item.child(1).attr("href");
//                positions.put(position, url);
//            }
//        }
//        return positions;
//
    }

    private static boolean isException(String text) {
        return text.contains("youtube.com") || text.contains("smotri.com");
    }
}
